<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1.创建ajax对象
        let ajax = new XMLHttpRequest();

        // 2.配置ajax请求信息
        ajax.open('post', '/index');

        // 3.设置参数格式请求头
        ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // 按照参数格式传参
        ajax.send('username=小花&age=18');
        
        // 3.设置对象格式请求头
        // ajax.setRequestHeader('Content-Type', 'application/json');
        // 按照对象格式传参，
        // ajax.send('{"username":"小黑","age":20}')

        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                console.log(ajax.responseText);
            }
        }
    </script>
</body>

</html>